<style type="text/css">
#test_source_urls .source_url{padding:5px;}
#test_source_urls p{margin:0;}
#test_source_urls ol{padding-left:15px;}
</style>
<div id="test_source_urls">
{if condition="empty($source_urls)"}
	没有起始页网址
{else /}
	{if condition="$config['source_is_url']"}
		<b>起始页已设置为内容页网址</b>
		{foreach $source_urls as $source=>$urls}
		<div class="source_url">
			<p>{$source}</p>
			<ul style="padding-left:15px;white-space:normal;">
			{if condition="empty($urls)"}
				<li>无</li>
			{elseif condition="is_array($urls)"}
				{foreach $urls as $url}
				<li>
					[<a href="javascript:;" class="cont_ceshi" url="{$url}">测试</a>]
					[<a href="javascript:;" class="cont_element" url="{$url}">元素</a>]
					{$url}
				</li>
				{/foreach}
			{else /}
				<li>
					[<a href="javascript:;" class="cont_ceshi" url="{$urls}">测试</a>]
					[<a href="javascript:;" class="cont_element" url="{$urls}">元素</a>]
					{$urls}
				</li>
			{/if}
			</ul>
		</div>
		{/foreach}
	{else /}
		{foreach name="source_urls" item="source_url"}
		<div class="source_url" url="{$source_url}">
			<p><b>抓取起始页面：</b>{$source_url}</p>
			<p class="cont_urls_num"></p>
			<div class="cont_urls_list">
			</div>
		</div>
		{/foreach}
	{/if}
{/if}
</div>
<script type="text/javascript">
'use strict';//严格模式
var max_num=5;//测试时最多显示数量
var url_ajax_requests=new Array();//ajax对象
$(document).ready(function(){
	$('#test_source_urls .source_url').each(function(index){
		//起始页开始测试
	{if condition="!$config['source_is_url']"}
		var source_url=$(this).attr('url');
		{if condition="!empty($config['level_urls'])"}
		//有多级
		if(index<max_num){
			//测试前几页
			get_cont_urls(source_url,$(this),1);
		}
		{else /}
		get_cont_urls(source_url,$(this),0);
		{/if}
	{/if}

		//测试
		$('#test_source_urls').on('click','.cont_ceshi',function(){
			var test_cont_url="{:url('Cpattern/test?op=cont_url&coll_id='.$collData['id'])}&cont_url=_url_";
			test_cont_url=test_cont_url.replace('_url_',encodeURIComponent($(this).attr('url')));
			var urls={};
			$(this).parents('.source_url').each(function(){
				var surl=$(this).attr('url');
				if(surl){
					if($(this).attr('level')){
						//是级别
						urls['level_'+$(this).attr('level')]=surl;
					}else{
						//是起始页
						urls['source_url']=surl;
					}
				}
			});
			if(urls){
				//加上多级和起始网址
				for(var i in urls){
					test_cont_url+='&'+i+'='+encodeURIComponent(urls[i]);
				}
			}
			window.open(test_cont_url,'_test_cont');
		});

		//元素
		$('#test_source_urls').on('click','.cont_element',function(){
			var ele_cont_url="{:url('Cpattern/test?op=elements&coll_id='.$collData['id'])}&cont_url=_url_";
			ele_cont_url=ele_cont_url.replace('_url_',encodeURIComponent($(this).attr('url')));
			window.open(ele_cont_url,'_test_cont');
		});
	});
	//展开收起
	$('#test_source_urls').on('click','.cont_urls_num a',function(){
		var status=$(this).attr('status');
		if(status==1){
			$(this).html('[展开]');
			$(this).attr('status',0);
			$(this).parent().siblings('.cont_urls_list').hide();
		}else{
			$(this).html('[收起]');
			$(this).attr('status',1);
			$(this).parent().siblings('.cont_urls_list').show();
		}
	});

	//获取网址
	function get_cont_urls(source_url,curObj,level){
		if(source_url){
			curObj.children('.cont_urls_list').html('<img src="'+window.site_config.pub+'/static/images/load1.gif">').show();
			
			var url="{:url('Cpattern/test?op=cont_urls&coll_id='.$collData['id'].'&source_url=_url_&level=_level_')}";
			url=url.replace('_url_',encodeURIComponent(source_url)).replace('_level_',level);
			var url_ajax_request=$.ajax({
				type:'get',
				url:url,
				async:true,
				success:function(data){
					if(data.code==1){
						//内容网址列表
						var urls=data.data.urls;
						var list='<ul style="list-style-type:'+(level>0?'decimal':'square')+';padding-left:15px;white-space:normal;overflow-x:scroll;">';
						if(level>0){
							//抓取级别网址
							for(var i in urls){
								if(i<max_num){
									//测试前几页
									list+='<li class="source_url" url="'+urls[i]+'" level="'+level+'"><p><b>抓取第'+level+'级页面 “'+data.data.levelName+'”：</b>'+urls[i]+'</p><p class="cont_urls_num"></p><div class="cont_urls_list"></div></li>';
								}
							}
							list+='</ul>';
							
							curObj.children('.cont_urls_list').html(list).show();
							curObj.children('.cont_urls_num').html('获取到'+urls.length+'条网址'+(urls.length>max_num?'（只测试前'+max_num+'条）':'')+' <a href="javascript:;" status="1">[收起]</a>');

							curObj.children('.cont_urls_list').find('.source_url').each(function(){
								var obj=$(this);
								get_cont_urls(obj.attr('url'),obj,data.data.nextLevel);//抓取下一级或内容页
							});
							
						}else{
							//无级别，显示测试网址
							for(var i in urls){
								list+='<li>[<a href="javascript:;" class="cont_ceshi" url="'+urls[i]+'">测试</a>] '
									+'[<a href="javascript:;" class="cont_element" url="'+urls[i]+'">元素</a>] '+urls[i]+'</li>';
							}
							list+='</ul>';
							
							//没有下一级
							curObj.children('.cont_urls_list').html(list).hide();
							curObj.children('.cont_urls_num').html('获取到'+urls.length+'条网址 <a href="javascript:;" status="0">[展开]</a>');
						}
					}else{
						//错误信息
						curObj.children('.cont_urls_list').html('<span style="color:red">'+data.msg+'</span>').show();
					}
				},
				dataType:'json'
			});
			window.url_ajax_requests.push(url_ajax_request);
		}
	}
});
$('#myModal').on('hide.bs.modal', function (e) {
	if(window.url_ajax_requests){
		//退出时清除所有ajax请求
		for(var i in window.url_ajax_requests){
			window.url_ajax_requests[i].abort();//终止ajax
		}
	}
});
</script>